<template>
  <div class="login">
    <s-header
      :name="type == 'login' ? '登录' : '注册'"
      :back="'/home'"
    ></s-header>
    <img
      class="logo"
      src="http://phonecard1.gzyezi.top/img/content.0fb17873.jpeg"
      alt=""
    />
    <div v-if="type == 'login'" class="login-body login">
      <van-form ref="phoneField" @submit="onSubmit">
        <van-field
          v-model="username"
          name="phoneNumber"
          label="电话"
          placeholder="电话"
          :rules="[{ required: true, message: '请填写电话' }]"
        />
        <van-field
          center
          clearable
          label="验证码"
          placeholder="输入验证码"
          v-model="verify"
        >
          <template #button>
            <!-- <vue-img-verify ref="verifyRef" /> -->
            <van-button type="primary" @click="getVerifyCode"
              >点击发送验证码</van-button
            >
          </template>
        </van-field>
        <div style="margin: 16px">
          <van-button round block color="#1baeae" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
       <nav-bar></nav-bar>
  </div>
</template>

<script>
import axios from 'axios'
import { reactive, ref, toRefs } from "vue";
import sHeader from "@/components/SimpleHeader";
import vueImgVerify from "@/components/VueImageVerify";
import { useRouter } from "vue-router"; 
import navBar from "@/components/NavBar";

import { login, register, getVerifyPhoneCode } from "@/service/user";
import { setLocal } from "@/common/js/utils";
import md5 from "js-md5";
import { Toast } from "vant";
export default {
  setup() {
    const verifyRef = ref(null);
    const phonefield = ref(null);
    const state = reactive({
      username: "",
      password: "",
      username1: "",
      password1: "",
      type: "login",
      imgCode: "",
      verify: "",
      isSend:false,
    });

    // 切换登录和注册两种模式
    const toggle = (v) => {
      state.type = v;
      state.verify = "";
    };

    // 提交登录或注册表单
    const router = useRouter();
    

    
    const onSubmit = async (values) => { 
          await login( 
          {
          mobile: state.username,
          smsCode:state.verify,
        }).then((rep)=>{
          rep=rep.data;
          setLocal("token", rep.userToken);
          console.log(rep.userToken);
          
          setLocal("userinfo", JSON.stringify(rep));
           router.push({ name: 'home'});

           axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
            axios.defaults.headers['userToken'] = localStorage.getItem('token') || ''
            axios.defaults.headers['userId'] =  rep.id
        });
        // setLocal("token", data);
        // 需要刷新页面，否则 axios.js 文件里的 token 不会被重置
        // window.location.href = "/";
      // } else {
      //   await register({
      //     loginName: values.username1,
      //     password: values.password1,
      //   });
      //   Toast.success("注册成功");
      //   state.type = "login";
      //   state.verify = "";
      // }
    };
    const getVerifyCode =   () => {
      if (!state.username) {
        Toast.fail("电话都不填，盲人？");
        return;
      }
      if (state.isSend) {
        Toast.fail("赶着去投胎？");
        return;
      }
       state.isSend=true;
        getVerifyPhoneCode({
        mobile: state.username,
      }).then((rep) => {
        if (rep.success) {
          Toast.success("发送成功"); 
          setTimeout(()=>{state.isSend=false},2000)
        } 
      }).catch(()=>{
         state.isSend=false; 
      });
    };

    return {
      getVerifyCode,
      ...toRefs(state),
      toggle,
      onSubmit,
      verifyRef,
      phonefield,
    };
  },
  components: {
    sHeader,
    vueImgVerify,
    navBar
  },
};
</script>

<style lang="less">
.login {
  .logo {
    width: 120px;
    height: 120px;
    display: block;
    margin: 80px auto 20px;
  }
  .login-body {
    padding: 0 20px;
  }
  .login {
    .link-register {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: inline-block;
    }
  }
  .register {
    .link-login {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: inline-block;
    }
  }
  .verify-bar-area {
    margin-top: 24px;
    .verify-left-bar {
      border-color: #1baeae;
    }
    .verify-move-block {
      background-color: #1baeae;
      color: #fff;
    }
  }
  .verify {
    > div {
      width: 100%;
    }
    display: flex;
    justify-content: center;
    .cerify-code-panel {
      margin-top: 16px;
    }
    .verify-code {
      width: 40% !important;
      float: left !important;
    }
    .verify-code-area {
      float: left !important;
      width: 54% !important;
      margin-left: 14px !important;
      .varify-input-code {
        width: 90px;
        height: 38px !important;
        border: 1px solid #e9e9e9;
        padding-left: 10px;
        font-size: 16px;
      }
      .verify-change-area {
        line-height: 44px;
      }
    }
  }
}
</style>
